import {Component, OnInit} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {FetchService} from '../../../core/net/fetch.service';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-examine-object-list',
  templateUrl: './examine-object-list.component.html',
  styleUrls: ['./examine-object-list.component.scss']
})
export class ExamineObjectListComponent implements OnInit {
  searchForm: FormGroup;
  isAllDisplayDataChecked = false;
  isIndeterminate = false;
  listOfDisplayData = [];
  listOfAllData = [];
  allChecked = false;
  indeterminate = false;
  delDisable = true;
  mapOfCheckedId: { [key: string]: boolean } = {};
  numberOfChecked = 0;
  yearOption = [
    {value: '', label: '全部'},
    {value: 2019, label: '2019'},
    {value: 2020, label: '2020'},
  ];
  levelOption = [
    {value: '', label: '全部'},
    {value: '1', label: '类型一'},
    {value: '2', label: '类型二'},
  ];
  form: FormGroup;
  modalTitle: string = '';
  modalShow: boolean = false;
  nzBodyStyle: any = {
    padding: '16px 20px 0'
  };

  constructor(private fb: FormBuilder,
              private fetchService: FetchService,
              private message: NzMessageService) {
    this.searchForm = this.fb.group({
      name: [''],
      year: ['2019'],
      level: ['']
    });

    this.form = this.fb.group({
      name: ['', Validators.required],
      address: ['', Validators.required],
      chargeman: ['', Validators.required],
      officeAccount: [[''], Validators.required],
      xzqh: [''],
      phone: ['', Validators.required],
      area: ['', Validators.required],
      channelAccount: ['', Validators.required],
      remark: [[''], Validators.required]
    });
  }

  ngOnInit(): void {
    this.queryInfo();
  }

  queryInfo(): void {
    this.fetchService.post<any>('archives/wrsWsEvent/list', {}).subscribe(res => {
      this.listOfAllData = [{
        "id": 1,
        "name": "唐徕渠管理处",
        "address": "唐徕渠管理处",
        "chargeman": "张三",
        "phone": "18037653827",
        "area": "1237",
        "officeAccount": 12,
        "channelAccount": 8,
        "remark": "",
        "xzqh": 420000
      }];
    });
  }

  currentPageDataChange($event): void {
    this.listOfDisplayData = $event;
    this.refreshStatus();
  }

  refreshStatus(): void {
    this.delDisable = Object.keys(this.mapOfCheckedId).length == 0;
    this.isAllDisplayDataChecked = this.listOfDisplayData
      .every(item => this.mapOfCheckedId[item.id]);
    this.isIndeterminate =
      this.listOfDisplayData.some(item => this.mapOfCheckedId[item.id]) &&
      !this.isAllDisplayDataChecked;
    this.numberOfChecked = this.listOfAllData.filter(item => this.mapOfCheckedId[item.id]).length;
  }

  checkAll(value: boolean): void {
    this.listOfDisplayData.forEach(item => (this.mapOfCheckedId[item.id] = value));
    this.refreshStatus();
  }

  openEditModal(item?): void {
    this.modalTitle = item ? `编辑` : '新增';
    this.form.patchValue(item || {});
    this.modalShow = true;
  }


  onDelete(id) {
    this.fetchService.post('archives/wrsProjectInfo/del', { id }).subscribe(resp => {
      this.message.info('删除成功');
      this.queryInfo();
    });
  }

  onDeleteAll(): void {
    const ids = Object.keys(this.mapOfCheckedId);
  }

  onConfirm() {
    if (!this.form.valid) {
      this.message.warning('信息填写有误');
      return;
    }
    const data = this.form.value;
    if (data.id) {
      this.fetchService.post('archives/wrsProjectInfo/edit', data).subscribe(res => {
        this.modalShow = false;
        this.queryInfo();
      });
    } else {
      this.fetchService.post('archives/wrsProjectInfo/add', data).subscribe(res => {
        this.modalShow = false;
        this.queryInfo();
      });
    }
  }

  onAdd(): void {
    this.form.reset();
    this.openEditModal();
  }

  onCancel() {
    this.modalShow = false;
  }
}
